package org.backend.canteenbackend.controller;

import org.backend.canteenbackend.common.CommonResponse;
import org.backend.canteenbackend.entity.Languages;
import org.backend.canteenbackend.entity.Product;
import org.backend.canteenbackend.service.CatalogService;
import org.backend.canteenbackend.vo.CategoryVO;
import org.backend.canteenbackend.vo.ItemSimpleVO;
import org.backend.canteenbackend.vo.ItemVO;
import org.backend.canteenbackend.vo.ProductVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/api/")
public class CatalogController {
    @Autowired
    private CatalogService catalogService;

    //获取单个类以及类中的产品
    @GetMapping("catalog/{categoryId}")
    @ResponseBody
    public CommonResponse<CategoryVO> getCategory(@PathVariable("categoryId") String categoryId){
        return catalogService.getCategory(categoryId);
    }

    //获取单个商品以及其中的产品和其所属的类
    @GetMapping("product/categoryId/{productId}")
    @ResponseBody
    public CommonResponse<ProductVO> getProduct(@PathVariable("productId") String productId){
        return catalogService.getProduct(productId);
    }

    //获取单个商品以及其中的产品和其所属的类
    @GetMapping("item/{productId}")
    @ResponseBody
    public CommonResponse<ItemVO> getItem(@PathVariable("productId") String itemId){
        return catalogService.getItem(itemId);
    }

    //收藏商品列表
    @GetMapping("/favorites")
    @ResponseBody
    public CommonResponse<List<ItemSimpleVO>> getUserFavorites() {
        // 模拟获取收藏列表
        List<ItemSimpleVO> favoriteItems = List.of(
                new ItemSimpleVO("101", "Pizza"),
                new ItemSimpleVO("102", "Burger"),
                new ItemSimpleVO("103", "Sushi")
        );

        return CommonResponse.createForSuccess(favoriteItems);
    }

    //语言
    @GetMapping("languages")
    @ResponseBody
    public CommonResponse<List<Languages>> getSelectLanguage(){
        List<Languages> languages = catalogService.getAllLanguages();
        return CommonResponse.createForSuccess(languages);
    }

    //更新语言设置
    @PostMapping("user/language")
    @ResponseBody
    public CommonResponse<String> setUserLanguage(@RequestBody Map<String, String> request, @RequestParam String userId) {
        String language = request.get("language");

        if (language == null || language.isEmpty()) {
            return CommonResponse.createForError("Language parameter is missing.");
        }

        boolean updated = catalogService.updateUserLanguage(userId, language);

        if (updated) {
            return CommonResponse.createForSuccessMessage("Language preference updated.");
        } else {
            return CommonResponse.createForError("Failed to update language preference.");
        }
    }


    //搜索功能
    @GetMapping("/search/{keyword}")
    @ResponseBody
    public CommonResponse<List<Product>> searchProducts(@PathVariable("keyword") String keyword) {
        List<Product> productList = catalogService.searchProductList(keyword);
        return CommonResponse.createForSuccess(productList);
    }

}